/**
 * Copyright (C) 2011-20077
 * 设计：		Zhangsk
 * 创建时间：	2011-11-27
 * Flash视频会议核心库 视频控制器类
 *
 * 
 */

package com.treemon.meetingcore.session
{
	import com.treemon.meetingcore.event.MeetingEvent;
	import com.treemon.meetingcore.event.MeetingVideoEvent;
	
	import com.xlib.xml.xmlhelper;

	public class VideoControl extends ControlBase
	{
		{
			trace("VideoControl");
		}
		
		public function VideoControl(aSession: MeetingSession)
		{
			super(aSession);
		}
		
		override protected function addListener(): void
		{
			//使用MeetingEnter暂时测试打开视频
			//_session.addEventListener(MeetingEvent.MeetingEnter, handleMeetingEnter);
			
			//_session.addEventListener(MeetingEvent.Capability, handleCapability);
			_session.addEventListener(MeetingEvent.MMTransOk, handleTransOk);
			_session.addEventListener(MeetingEvent.VideoAccept, handleVideoAccept);
			_session.addEventListener(MeetingEvent.VideoClose, handleVideoClose);
			
		} 
		
		private function handleTransOk(aEvent: MeetingEvent): void
		{
			
            //var s: String = mx.utils.StringUtil.substitute(MeetingEvent.MMTransOkMsg, _session.myJID,_session.myMMID);
			// '<iq id="{0}" to="{1}" type="get"><query xmlns="jabber:iq:agents"/></iq>';
			/*
<iq type="set" to="guest@masterjadc2s_1.machine1.com/8E34AF88-0310-132D-5599-103855164899" from="530345@slavemcu_1.machine1.com" xmlns="jabber:client">
  <query xmlns="meeting:conf:statusTransferOk">
    <userDes mmid="01000000-00000009"/>
  </query>
</iq>	
			*/
			var xmlQuery: XML = aEvent.xml.children()[0];
            var ns: Namespace = xmlQuery.namespace();
            
            if (xmlQuery.ns::userDes.@mmid == _session.myMMID)
            {
                var strFrom: String = aEvent.xml.@from;
                /*
				var s: String = '<iq type="set" to="{0}" from="{1}" xmlns="jabber:client">' +
					'<query xmlns="meeting:conf:statusTransferOk">' + 
                    '<userDes mmid="{2}"/></query></iq>';
    			s = mx.utils.StringUtil.substitute(s, _session.myJID, strFrom, _session.myMMID);
    			//s = mx.utils.StringUtil.substitute(s, "575751@slavemcu_1.machine1.com/&lt;flash&gt;", strFrom, _session.myMMID);
                //注意： XML对象对其值中的<zhangsk>，解析为&lt;zhangsk>
                //     XMLDocument会将其转为&lt;zhangsk&gt;
				
				trace(s);
				var oDoc: XMLDocument = new XMLDocument(s);
				_session.sendXML(oDoc.toString());
				*/
				
				
				
				var xmlIQ: XML = 
					<iq type="set" to="" from="" xmlns="jabber:client">
						<query xmlns="meeting:conf:statusTransferOk">
							<userDes mmid=""/>
						</query>
					</iq>;
				
				//todo: <zhangsk>  --->   &lt;flash&gt;
				xmlIQ.@to = _session.myJID;
				xmlIQ.@from = strFrom;
				xmlQuery = xmlIQ.children()[0];
				trace(xmlQuery.toXMLString());
				ns = xmlQuery.namespace();
				xmlQuery.ns::userDes.@mmid = _session.myMMID;
				trace(xmlIQ.toXMLString());
				var strXML: String = xmlhelper.getSafeXMLString(xmlIQ);
				trace(strXML);
				_session.sendXML(strXML);
			}

		}
		
		/*
		private function handleMeetingEnter(evt: MeetingEvent):void 
		{
			trace('VideoControl.handleMeetingEnter');
			var evtVideoOpen: MeetingVideoEvent = new MeetingVideoEvent(MeetingVideoEvent.Open);	
			evtVideoOpen.xml = XML("<iq><query><a>rtmp://192.168.1.113:1935/live</a><b>gbt_video</b></query></iq>");
			//dispatchEvent( evtVideoOpen );	//VideoTest.mxml接收不到
			_session.dispatchEvent(evtVideoOpen);		d	//VideoTest.mxml可以接收
		}
		*/
		
		private function handleCapability(aEvent: MeetingEvent): void
		{
			trace(aEvent.xml.toXMLString());
			var xmlQuery: XML = aEvent.xml.children()[0];
            var ns: Namespace = xmlQuery.namespace();
            //_session.myMMID = xmlQuery.ns::protDesdd.@mmid;
            var strVSend: String = xmlQuery.ns::user.ns::vSend;
            trace(strVSend);
            if ("immediately" == strVSend)
            {
	            var strRTMP: String = xmlQuery.ns::user.ns::rtmp;
	            
	            var evtVideoAccept: MeetingVideoEvent = new MeetingVideoEvent(MeetingVideoEvent.VideoAccept);
	            var nIndex: int = strRTMP.lastIndexOf("/");
	            evtVideoAccept.serverid = strRTMP.substring(0, nIndex);
	            evtVideoAccept.streamid = strRTMP.substring(nIndex + 1);
	            _session.dispatchEvent(evtVideoAccept);
	            
            }
		}
		
		private function handleVideoAccept(aEvent: MeetingEvent): void 
		{
			trace('VideoControl.handleVideoAccept');
/*
<iq from="111224@slavemcu_1.machine1.com" id="" type="set" to="guest@flashgw_1.machine1.com/{0A52F94F-CCCB-4554-8A5C-BAB710121172}" xmlns="jabber:client">
  <query xmlns="meeting:video:accept">
    <rtmp>rtmp://192.168.1.101:1935/live/test1</rtmp>
  </query>
</iq>
*/
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			var strRTMP: String = xmlQuery.ns::rtmp;
			
			var evtVideoAccept: MeetingVideoEvent = new MeetingVideoEvent(MeetingVideoEvent.VideoAccept);		
			var nIndex: int = strRTMP.lastIndexOf("/");
			evtVideoAccept.serverid = strRTMP.substring(0, nIndex);
			evtVideoAccept.streamid = strRTMP.substring(nIndex + 1);
			
			_session.dispatchEvent(evtVideoAccept);
		}
		
		private function handleVideoClose(aEvent: MeetingEvent): void
		{

			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			var strRTMP: String = xmlQuery.ns::rtmp;
			
			var evtVideoClose: MeetingVideoEvent = new MeetingVideoEvent(MeetingVideoEvent.VideoClose);
			var nIndex: int = strRTMP.lastIndexOf("/");
			evtVideoClose.serverid = strRTMP.substring(0, nIndex);
			evtVideoClose.streamid = strRTMP.substring(nIndex + 1);
			_session.dispatchEvent(evtVideoClose);
		}
			

	}
}